package com.lxm.ymcc.web.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lxm.ymcc.dto.*;
import com.lxm.ymcc.service.ICourseService;
import com.lxm.ymcc.domain.Course;
import com.lxm.ymcc.query.CourseQuery;
import com.lxm.ymcc.result.JSONResult;
import com.lxm.ymcc.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;


    /**
     //    * 保存和修改公用的
     //    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody @Valid  CousreParmDto dto){
            courseService.insert(dto);
        return JSONResult.success();
    }

//    /**
//    * 保存和修改公用的
//    */
//    @RequestMapping(value="/save",method= RequestMethod.POST)
//    public JSONResult saveOrUpdate(@RequestBody Course course){
//        if(course.getId()!=null){
//            courseService.updateById(course);
//        }else{
//            courseService.insert(course);
//        }
//        return JSONResult.success();
//    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        courseService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(courseService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(courseService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody CourseQuery query){
        Page<Course> page = new Page<Course>(query.getPage(),query.getRows());
        EntityWrapper<Course> wrapper = new EntityWrapper<>();
        // 模糊查询
        wrapper.like("name",query.getKeyword());
        page = courseService.selectPage(page,wrapper);
        return JSONResult.success(new PageList<Course>(page.getTotal(),page.getRecords()));
    }

    /**
     * 课程发布
     */
    @RequestMapping(value="/onLineCourse/{id}",method=RequestMethod.POST)
    public JSONResult onLineCourse(@PathVariable("id") Long id){
        courseService.onLineCourse(id);
        return JSONResult.success();
    }
    /**
     * 课程发布
     */
    @RequestMapping(value="/offLineCourse/{id}",method=RequestMethod.POST)
    public JSONResult offLineCourse(@PathVariable("id") Long id){
        courseService.offLineCourse(id);
        return JSONResult.success();
    }

    //邀请码
    @GetMapping("/listForUser")
    public JSONResult listForUser(){
        InvitationCodeDto invitationCodeDto = new InvitationCodeDto();
        return JSONResult.success(courseService.listForUser(invitationCodeDto));
    }




    /**
     * 课程资源显示
     */
    @RequestMapping(value="/detail/data/{id}",method= RequestMethod.GET)
    public JSONResult courseDetail(@PathVariable("id")   Long id ){
       CourseDetailVo detailVo =  courseService.courseDetail(id);
        return JSONResult.success(detailVo);
    }
    @GetMapping(value = "/courseState/{id}")
    public JSONResult courseStateById(@PathVariable Long id){
        CourseStateVo courseStateVo = courseService.courseStateById(id);
        return JSONResult.success(courseStateVo);
    }



    /**
     * 支付订单
     */
    @GetMapping(value = "/info/{id}")
    public JSONResult courseInfo(@PathVariable("id") String id){
        CourseOrderDataVo courseOrderDataVo = courseService.courseInfo(id);
        return JSONResult.success(courseOrderDataVo);
    }

}
